package com.ganbo.common.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Locale;

/**
 * 字符串工具类
 *
 * @author: GanBo
 * @date: 2023/6/1 11:18
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * 下划线转驼峰(例如: user_name --> userName)
     */
    public static String toCamelCase(String s) {
        if (s == null) return null;
        s = s.toLowerCase();
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '_') {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * tsCode 转 code
     *
     * @param tsCode
     * @return
     */
    public static String tsCodeToCode(String tsCode) {
        return tsCode.substring(0, tsCode.lastIndexOf("."));
    }

    /**
     * tushareTsCode转Ig507的dm(300803.SZ -> sz300803)
     *
     * @param tsCode
     * @return
     */
    public static String tsTsCodeToIg507Dm(String tsCode) {
        String result = StringUtils.EMPTY;
        if (StringUtils.isBlank(tsCode)) return result;
        String[] split = tsCode.split("\\.");
        if (split.length == 2) {
            return split[1].toLowerCase(Locale.ROOT) + split[0];
        }
        return "";
    }

    /**
     * 获取异常堆栈信息
     *
     * @param e
     * @return
     */
    public static String getStackTrace(Exception e) {
        StringBuffer sb = new StringBuffer();
        StringWriter stringWriter = new StringWriter();
        e.printStackTrace(new PrintWriter(stringWriter));
        sb.append(stringWriter);
        return sb.toString();
    }

}

